import cloudbase from '@cloudbase/js-sdk'

class CloudBaseAPI {
  constructor() {
    // 请替换为你的云开发环境ID
    this.app = cloudbase.init({
      env: '' // 需要替换为实际的环境ID
    })
  }

  // 管理员登录
  async adminLogin(username, password) {
    try {
      const result = await this.app.callFunction({
        name: 'adminAuth',
        data: { username, password }
      })
      return result.result
    } catch (error) {
      throw new Error('登录请求失败: ' + error.message)
    }
  }

  // 获取用户列表
  async getUserList(page = 1, limit = 20, search = '') {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetUsers',
        data: { 
          action: 'list',
          page, 
          limit, 
          search,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('获取用户列表失败: ' + error.message)
    }
  }

  // 更新用户信息
  async updateUser(userId, userData) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetUsers',
        data: { 
          action: 'update',
          userId,
          userData,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('更新用户信息失败: ' + error.message)
    }
  }

  // 修改用户状态
  async changeUserStatus(userId, status) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetUsers',
        data: { 
          action: 'changeStatus',
          userId,
          userData: { status },
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('修改用户状态失败: ' + error.message)
    }
  }

  // 获取活动列表
  async getActivityList(page = 1, limit = 20, search = '') {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetActivities',
        data: { 
          action: 'list',
          page, 
          limit, 
          search,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('获取活动列表失败: ' + error.message)
    }
  }

  // 创建活动
  async createActivity(activityData) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetActivities',
        data: { 
          action: 'create',
          activityData,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('创建活动失败: ' + error.message)
    }
  }

  // 创建活动
  async createActivity(activityData) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetActivities',
        data: { 
          action: 'create',
          activityData,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('创建活动失败: ' + error.message)
    }
  }

  // 更新活动
  async updateActivity(activityId, activityData) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetActivities',
        data: { 
          action: 'update',
          activityId,
          activityData,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('更新活动失败: ' + error.message)
    }
  }

  // 删除活动
  async deleteActivity(activityId) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetActivities',
        data: { 
          action: 'delete',
          activityId,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('删除活动失败: ' + error.message)
    }
  }

  // 修改活动状态
  async changeActivityStatus(activityId, status) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetActivities',
        data: { 
          action: 'changeStatus',
          activityId,
          activityData: { status },
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('修改活动状态失败: ' + error.message)
    }
  }

  // 获取活动报名列表
  async getActivityRegistrations(activityId) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetActivities',
        data: { 
          action: 'getRegistrations',
          activityId,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('获取活动报名列表失败: ' + error.message)
    }
  }

  // 获取用户活动参与记录
  async getUserActivities(userId) {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetUsers',
        data: { 
          action: 'getUserActivities',
          userId,
          adminToken 
        }
      })
      return result.result
    } catch (error) {
      throw new Error('获取用户活动记录失败: ' + error.message)
    }
  }

  // 获取统计数据
  async getStatistics() {
    const adminToken = localStorage.getItem('adminToken')
    try {
      const result = await this.app.callFunction({
        name: 'adminGetStatistics',
        data: { adminToken }
      })
      return result.result
    } catch (error) {
      throw new Error('获取统计数据失败: ' + error.message)
    }
  }

  // 获取云存储临时URL
  async getTempFileURL(fileID) {
    try {
      if (!fileID || !fileID.startsWith('cloud://')) {
        return fileID
      }
      
      // 使用云开发的getTempFileURL方法
      const auth = this.app.auth({
        persistence: 'local'
      })
      
      const result = await this.app.callFunction({
        name: 'getTempFileURL',
        data: { fileID }
      })
      
      if (result.result && result.result.success) {
        return result.result.tempFileURL
      }
      
      return fileID
    } catch (error) {
      console.error('获取临时URL失败:', error)
      return fileID
    }
  }
}

export default new CloudBaseAPI()